<!DOCTYPE html>
<html>
<head>
<title>Circuit Diagram</title>
</head>
<body>
<canvas id="circuitCanvas" width="500" height="400" style="background-color: white;"></canvas>
<script>
    const canvas = document.getElementById('circuitCanvas');
    const ctx = canvas.getContext('2d');
    
    // Style settings
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 2;
    ctx.font = '20px Arial';
    ctx.textAlign = 'center';

    // Define circuit dimensions and component positions
    const x1 = 100;
    const y1 = 80;
    const x2 = 400;
    const y2 = 280;
    const centerX = (x1 + x2) / 2;
    const centerY = (y1 + y2) / 2;

    // --- Draw Components ---

    // Resistor (R)
    const resWidth = 80;
    const resHeight = 30;
    const resX = centerX;
    const resY = y2;
    ctx.beginPath();
    // Draw the rectangle for the resistor. We draw it filled with white to break the line underneath, then stroke it.
    ctx.fillStyle = 'white';
    ctx.fillRect(resX - resWidth / 2, resY - resHeight / 2, resWidth, resHeight);
    ctx.fillStyle = 'black';
    ctx.rect(resX - resWidth / 2, resY - resHeight / 2, resWidth, resHeight);
    ctx.stroke();
    ctx.fillText('R', resX, resY + resHeight / 2 + 25);

    // Capacitor (C)
    const capPlateLength = 40;
    const capPlateGap = 12;
    const capX = centerX;
    const capY = y1;
    ctx.beginPath();
    // Plate 1
    ctx.moveTo(capX - capPlateGap / 2, capY - capPlateLength / 2);
    ctx.lineTo(capX - capPlateGap / 2, capY + capPlateLength / 2);
    // Plate 2
    ctx.moveTo(capX + capPlateGap / 2, capY - capPlateLength / 2);
    ctx.lineTo(capX + capPlateGap / 2, capY + capPlateLength / 2);
    ctx.stroke();
    ctx.fillText('C', capX, capY - capPlateLength / 2 - 15);

    // Switch (S)
    const switchX = x1;
    const switchGap = 30;
    // Terminals
    const term1Y = centerY - switchGap / 2;
    const term2Y = centerY + switchGap / 2;
    ctx.beginPath();
    // Blade (from lower terminal, open to the left)
    ctx.moveTo(switchX, term2Y);
    ctx.lineTo(switchX - 35, term1Y); 
    ctx.stroke();

    // --- Draw Wires ---
    ctx.beginPath();
    // Top wire
    ctx.moveTo(x1, y1);
    ctx.lineTo(capX - capPlateGap / 2, y1);
    ctx.moveTo(capX + capPlateGap / 2, y1);
    ctx.lineTo(x2, y1);

    // Right wire
    ctx.moveTo(x2, y1);
    ctx.lineTo(x2, y2);

    // Bottom wire
    ctx.moveTo(x2, y2);
    ctx.lineTo(resX + resWidth / 2, y2);
    ctx.moveTo(resX - resWidth / 2, y2);
    ctx.lineTo(x1, y2);

    // Left wire (with switch)
    ctx.moveTo(x1, y2);
    ctx.lineTo(x1, term2Y); // Wire to lower terminal
    ctx.moveTo(x1, term1Y); // Wire from upper terminal
    ctx.lineTo(x1, y1);
    ctx.stroke();


    // --- Draw Figure Label ---
    ctx.font = 'bold 22px Arial';
    ctx.fillText('Fig. 6.1', centerX, y2 + 70);

</script>
</body>
</html>